package com.example.test_activity.MVP.presenter;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.example.test_activity.MVP.view.ILogin;
import com.example.test_activity.http.HttpUtil;
import com.example.test_activity.utils.DecideLogin;
import com.example.test_activity.utils.GetUser;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * 实现LoginActivity的业务逻辑
 * Created by 旭 on 2017/5/5/005.
 */

public class LoginPresenter implements ILoginPresenter {

    private ILogin loginView;
    private DecideLogin decideLogin = new DecideLogin();
    private GetUser getuser = new GetUser();

    public LoginPresenter(ILogin loginView) {
        this.loginView = loginView;
    }

    @Override
    public void doLogin(String account, String password, String code) {
        Map<String,String> map = new HashMap<>();
        map.put("zjh1", "");
        map.put("tips", "");
        map.put("lx", "");
        map.put("evalue","");
        map.put("eflag", "");
        map.put("fs", "");
        map.put("dzslh", "");
        map.put("zjh",account);
        map.put("mm",password);
        map.put("v_yzm",code);

        HttpUtil.getInstance().sendGetPost().login(map)
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody responseBody) throws Exception {
                        String html = responseBody.string();
                        boolean decide = decideLogin.decide(html);
                        String user;
                        if (decide)
                        {
                            user = getuser.getUser();
                        }else {
                            user = "";
                        }
                        return user;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull String user) {
                        loginView.login(user);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("error",e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getCodeImg() {

        HttpUtil.getInstance().sendGetPost().getImage()
                .map(new Function<ResponseBody, Bitmap>() {
                    @Override
                    public Bitmap apply(@NonNull ResponseBody responseBody) throws Exception {
                        InputStream in = responseBody.byteStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(in);
                        in.close();

                        return bitmap;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Bitmap>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Bitmap bitmap) {

                        loginView.showCodeImg(bitmap);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("error", e.toString());
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    @Override
    public void doClear() {
        loginView.clear();
    }

    @Override
    public void destroy()
    {
        loginView = null;
    }
}
